// 包信息
package com.example.formdemo.slice;
// 导入模块
import com.example.formdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice {
    // 定义私有属性
    private TextField nameTextField;
    private RadioContainer genderRadio;
    private Checkbox hobbyRead;
    private Checkbox hobbySports;
    private Checkbox hobbyGame;
    private DatePicker datePicker;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 获取组件实例对象，赋值属性
        nameTextField = (TextField)findComponentById(ResourceTable.Id_textfield_name);
        genderRadio = (RadioContainer)findComponentById(ResourceTable.Id_radio_container);
        hobbyRead = (Checkbox)findComponentById(ResourceTable.Id_checkbox1);
        hobbySports = (Checkbox)findComponentById(ResourceTable.Id_checkbox2);
        hobbyGame = (Checkbox)findComponentById(ResourceTable.Id_checkbox3);
        datePicker = (DatePicker)findComponentById(ResourceTable.Id_date_pick);
        // 获取Button实例
        Button commitButton = (Button) findComponentById(ResourceTable.Id_commit_button);
        // 添加监听
        commitButton.setClickedListener((Component component) -> {
            // 提交前做检查
            // 1. 校验昵称不能为空
            String name = nameTextField.getText();
            if (name.length() == 0) {
                toastMsg("请输入姓名后提交");
                return;
            }
            // 2. 校验兴趣必选选择
            if (!hobbyGame.isChecked() && !hobbySports.isChecked() && !hobbyRead.isChecked()) {
                toastMsg("请至少选择一种兴趣爱好");
                return;
            }
            nameTextField.clearFocus();
            toastMsg("提交成功！");
        });
    }
    // 封装一个方法来弹出提示窗
    private void toastMsg(String info) {
        ToastDialog toastDialog = new ToastDialog(getContext());
        toastDialog.setText(info);
        toastDialog.setAlignment(LayoutAlignment.BOTTOM);
        toastDialog.show();
    }
}
